#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@File ：CartMapper.py
@Author ：zqy
@Email : zqingy@work@163.com 
@note: 
"""
from application.mapper import BaseMapper
from application.model.OrderModel import CartItem


class CartMapper(BaseMapper):
    model: CartItem = CartItem

    @classmethod
    def get_cart_item_by_user_id(cls, user_id: int, cart_ids: list = None) -> list:
        """
        获取购物车列表
        :param user_id: 用户id
        :return:
        """
        _filters = [cls.model.user_id == user_id]
        if cart_ids:
            _filters.append(cls.model.id.in_(cart_ids))
        items = cls.model.query.filter(*_filters).all()
        return [
            {
                'id': i.id,
                'product_id': i.product_id,
                'product_name': i.product.name,
                'image_url': i.product.image_url,
                'quantity': i.quantity,
                'selected': i.selected,
                'price': i.product.price,
                'usable': i.product.usable,
                'stock': i.product.stock,
                'total_price': round(i.product.price * i.quantity, 2)
            }
            for i in items
        ]
